<template>
	<div class="edit">
		<!-- 顶部返回 -->
		<van-nav-bar
		  title="修改地址"
		  left-arrow
		  @click-left="$router.go(-1)"
		/>
		<!-- 地址选择区  -->
		<van-address-edit
		  :area-list="areaList"
		  :address-info = "AddressInfo"
		  show-postal
		  show-delete
		  show-set-default
		  show-search-result
		  :search-result="searchResult"
		  :area-columns-placeholder="['请选择', '请选择', '请选择']"
		  @save="onSave"
		  @delete="onDelete"
		  @change-detail="onChangeDetail"
		/>
	</div>
</template>

<script>
	import PubSub from 'pubsub-js'
	import {mapState} from 'vuex'
	import {reqAddressApi,delAddress} from '@/api'
	import areaList from '../area'
	export default {
		name:'edit',
		data() {
		    return {
		      areaList,
		      searchResult: [],
			  AddressInfo : {}
		    }
		  },
		  async created() {
		  	let id = this.$route.query.id
			let {token} = this.isDenglu
			let res = await reqAddressApi(token)
			let index = res.data.data.findIndex(item=>item._id == id)
			let {
				address_phone:tel,
				province,
				address_name:name,
				city,
				county,
				address_area_detail:addressDetail,
				areaCode,
				address_post_code:postalCode,
				address_tag:isDefault,
				_id
			} = res.data.data[index]
			let list = {name,tel,province,city,county,addressDetail,areaCode,postalCode,isDefault,_id}
			this.AddressInfo = list
		  },
		  computed:{
			  ...mapState(['isDenglu']),
		  },
		  methods: {
		    onSave() {
		      Toast('save');
		    },
		    async onDelete(item,index) {
				let {_id} = item
				let res = await delAddress(_id)
				PubSub.publish("del_address",_id)
				this.$router.go(-1)
		    },
		    onChangeDetail(val) {
		      if (val) {
		        this.searchResult = [
		          {
		            name: '黄龙万科中心',
		            address: '杭州市西湖区',
		          },
		        ];
		      } else {
		        this.searchResult = [];
		      }
		    },
		  },
	}
</script>

<style scoped lang="less">
	.edit{
		position: fixed;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 1000;
		background-color: #fff;
	}
</style>